手写 Parcelable 序列化和反序列化的具体过程

1
2
3
4
5
6
binder 和 socket 通信的区别有哪些 :
binder 共享内存,Soket需要copy内存
Socket 远程,本地低速(zygote)


Serializable 和 Parcelable 之间的区别: (io流,共享内存)

Parcelable 序列化和反序列化的具体过程

Parcel.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package zzw.com.testparcel

public class Parcel {

companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}

private var mNativePtr: Long = 0

init {
mNativePtr = nativeCreate()
}


fun writeInt(value: Int) {
nativeWriteInt(mNativePtr, value)
}

fun readInt(): Int {
return nativeReadInt(mNativePtr)
}

fun setDataPosition(pos: Int) {
nativeSetDataPosition(mNativePtr, pos)
}

private external fun nativeCreate(): Long
private external fun nativeSetDataPosition(nativePtr: Long, pos: Int)

private external fun nativeWriteInt(nativePtr: Long, value: Int)
private external fun nativeReadInt(nativePtr: Long): Int


}

native-lib.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <jni.h>
#include <stdlib.h>
#include <string>


class Parcel {
char *mData;//共享内存首地址
int mDataPos = 0;//当前指针位置
public:
Parcel() {
mData = (char *) malloc(1024);
}

void writeInt(jint value) {
*reinterpret_cast<int *>(mData + mDataPos) = value;
mDataPos += sizeof(int);
}

void setDataPosition(jint i) {
mDataPos = i;
}

jint readInt() {
int result = *reinterpret_cast<int *>(mData + mDataPos);
mDataPos += sizeof(int);
return result;
}
};


extern "C" JNIEXPORT jlong
JNICALL
Java_zzw_com_testparcel_Parcel_nativeCreate(
JNIEnv *env,
jobject /* this */) {

return reinterpret_cast<jlong>(new Parcel());
}




extern "C" JNIEXPORT void
JNICALL
Java_zzw_com_testparcel_Parcel_nativeWriteInt(
JNIEnv *env,
jobject, jlong ptr, jint value) {
Parcel *parcel = reinterpret_cast<Parcel *>(ptr);
parcel->writeInt(value);
}

extern "C" JNIEXPORT void
JNICALL
Java_zzw_com_testparcel_Parcel_nativeSetDataPosition(
JNIEnv *env,
jobject, jlong ptr, jint pos) {
Parcel *parcel = reinterpret_cast<Parcel *>(ptr);
parcel->setDataPosition(pos);

}

extern "C" JNIEXPORT jint
JNICALL
Java_zzw_com_testparcel_Parcel_nativeReadInt(
JNIEnv *env,
jobject, jlong ptr) {
Parcel *parcel = reinterpret_cast<Parcel *>(ptr);
return parcel->readInt();
}

MainActivity.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package zzw.com.testparcel

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

var parcel = Parcel()

parcel.writeInt(5)
parcel.writeInt(10)

parcel.setDataPosition(0)

var num1 = parcel.readInt()
var num2 = parcel.readInt()

Log.e("zzz", "num1 =$num1 num2=$num2 ")
}

}

writeString分为两节,会先把长度存着,然后后面跟着具体的数据

-------------The End-------------